package com.tuanzi.loan.business.repository.family.impl;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.CurrentUser;
import com.tuanzi.loan.business.entity.common.CommProductChannel;
import com.tuanzi.loan.business.entity.family.LoanChannelNdInfo;
import com.tuanzi.loan.business.repository.family.custom.LargeAmountChannelManageRepositoryCustom;
import com.tuanzi.loan.core.repository.JOOQBaseRepositoryPlus;
import com.tuanzi.loan.generated.jooq.loan.tables.records.LoanChannelNdInfoRecord;
import com.tuanzi.loan.vo.request.family.LargeAmountChannelManageSearchRequest;
import com.tuanzi.loan.vo.response.family.LargeAmountChannelManageSearchResponse;
import com.tuanzi.server.loan_base.util.DateUtils;
import org.jooq.Record;
import org.jooq.RecordMapper;
import org.jooq.impl.TableImpl;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import java.sql.Timestamp;
import java.util.List;

import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_CHANNEL_ND_INFO;
import static com.tuanzi.loan.generated.jooq.loan.Tables.LOAN_LARGE_AMOUNT_PLUS;
import static com.tuanzi.loan.generated.jooq.loan.tables.LoanLargeAmountChannelManage.LOAN_LARGE_AMOUNT_CHANNEL_MANAGE;

@Repository
public class LargeAmountChannelManageRepositoryImpl  extends JOOQBaseRepositoryPlus implements LargeAmountChannelManageRepositoryCustom{
    @Override
    public Page<LargeAmountChannelManageSearchResponse> list(LargeAmountChannelManageSearchRequest searchRequest, Pageable pageable) {
        fields();
        contains(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.CHANNELNAME,searchRequest.getChannelName());
        eq(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.AMOUNTINTERVAL,searchRequest.getAmountInterval());
        return search(pageable,record -> {
            LargeAmountChannelManageSearchResponse response = new LargeAmountChannelManageSearchResponse();
            response.setId(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.ID));
            response.setAmountInterval(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.AMOUNTINTERVAL));
            response.setAppChannel(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.APPCHANNEL));
            response.setChannelName(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.CHANNELNAME));
            response.setMinAmount(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.MINAMOUNT));
            response.setMaxAmount(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.MAXAMOUNT));
            response.setPrdId(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.PRDID));
            response.setProductName(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.PRODUCTNAME));
            response.setUpdator(record.get(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.UPDATOR));
            return response;
        });
    }

    /*更改配置信息*/
    @Override
    public Integer updateByEntity(LargeAmountChannelManageSearchRequest request) {
        return dslContext.update(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE)
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.AMOUNTINTERVAL,request.getAmountInterval())
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.PRODUCTNAME,request.getProductName())
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.PRDID,request.getPrdId())
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.MAXAMOUNT,request.getMaxAmount())
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.MINAMOUNT,request.getMinAmount())
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.CHANNELNAME,request.getChannelName())
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.APPCHANNEL,request.getAppChannel())
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.UPDATE_TIME, Timestamp.valueOf(DateUtils.getCurrentTime()))
                .set(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.UPDATOR, CurrentUser.get().getName())
                .where(LOAN_LARGE_AMOUNT_CHANNEL_MANAGE.ID.eq(request.getId()))
                .execute();
    }

    @Override
    public List<String> selectChannel() {
        return dslContext.selectDistinct(LOAN_LARGE_AMOUNT_PLUS.C_FROM)
                .from(LOAN_LARGE_AMOUNT_PLUS)
                .where(LOAN_LARGE_AMOUNT_PLUS.C_FROM.isNotNull().and(LOAN_LARGE_AMOUNT_PLUS.C_FROM.notEqual(""))).fetch().into(String.class);
    }



    @Override
    public List<CommProductChannel> getChannelInfo(Integer prdid) {
        String sql ="select *from comm_product_channel where prdid="+prdid+" order by channel_code asc";
        return dslContext.fetch(sql).map(new RecordMapper<Record, CommProductChannel>() {
            @Override
            public CommProductChannel map(Record record) {
                CommProductChannel channel  = new CommProductChannel();
                channel.setChannelCode(record.get("channel_code",Integer.class));
                channel.setChannelName(record.get("channel_name",String.class));
                channel.setProductId(record.get("prdid",Integer.class));
                return channel;
            }
        });
    }

    @Override
    public Integer addChannelND(LoanChannelNdInfo info) {
        return dslContext.insertInto(LOAN_CHANNEL_ND_INFO)
                .columns(LOAN_CHANNEL_ND_INFO.CHANNEL_ND,
                        LOAN_CHANNEL_ND_INFO.CREATE_TIME,
                        LOAN_CHANNEL_ND_INFO.CREATOR,
                        LOAN_CHANNEL_ND_INFO.VERSION
                )
                .values(info.getChannel_nd(),
                        Timestamp.valueOf(DateUtils.getCurrentTime()),
                        CurrentUser.get().getName(),
                        0
                ).execute();
    }

    @Override
    public void addBatchChannelNd() {
        if (findCount()<10){
            List<LoanChannelNdInfoRecord> list = Lists.newArrayList();
            for (String str:selectChannel()) {
                LoanChannelNdInfoRecord info = new LoanChannelNdInfoRecord();
                info.setchannel_nd(str);
                info.setcreate_time(Timestamp.valueOf(DateUtils.getCurrentTime()));
                info.setcreator(CurrentUser.get().getName());
                list.add(info);
            }
            dslContext.batchInsert(list).execute();
        }
    }

    @Override
    public Integer findCount() {
        return dslContext.fetchCount(LOAN_CHANNEL_ND_INFO);
    }

    @Override
    public List<String> selectChannelInfo() {
        return dslContext.selectDistinct(LOAN_CHANNEL_ND_INFO.CHANNEL_ND)
                .from(LOAN_CHANNEL_ND_INFO)
                .where(LOAN_CHANNEL_ND_INFO.CHANNEL_ND.isNotNull().and(LOAN_CHANNEL_ND_INFO.CHANNEL_ND.notEqual(""))).fetch().into(String.class);
    }

    @Override
    protected TableImpl getTable() {
        return LOAN_LARGE_AMOUNT_CHANNEL_MANAGE;
    }
}
